<?php
/** 
 * Kernel class
 * This class implement actions. It processes applications
 *
 * @author Patrice Ferlet <metal3d@copix.org> 
 * @package core
 * @license http://opensource.org/licenses/lgpl-3.0.html GNU Lesser General Public License Version 3.0 (LGPLv3)
 */
include_once "libs/utils/standardobj.class.php";
include_once "libs/core/request.class.php";
include_once "libs/core/template.class.php";
include_once "libs/core/xml.class.php";
include_once "libs/core/process.class.php";
include_once "libs/utils/url.class.php";

include_once "libs/structs/actions.class.php";

include_once "libs/utils/debug.class.php";
include_once "libs/utils/exception.class.php";

include_once "libs/database/db.class.php";
include_once "libs/utils/shortcut.lib.php";


/**
 * Kernel class wich implements configuration and execute process
 *
 * This is a singleton pattern, you cannot use "new Kernel()". You need to
 * use Kernel::instance() or _kernel() shortcut
 * 
 */
class PKernel {

    static private $_instance=NULL;
    static private $configuration;
    static private $templatesvars;
    static private $params;

    /**
     * Constructor
     * Initialize project
     */
    private function __construct($configfile=null){
        //phpinfo();
        if ($configfile)
            PKernel::$configuration = new PXML($configfile);

        //check requests
        $module = _request('module','default');
        $group  = _request('group','default');
        $action = _request('action','default');

        $moduledir = PKernel::$configuration->get('root/paths')->name('modules@path');

        PKernel::$params['module'] = &$module;
        PKernel::$params['action'] = &$action;
        PKernel::$params['group'] = &$group;

        
    }

    /**
     * Do the stuff...
     */
    public function process(){
        //check module installation:
        PKernel::checkModuleInstallation();


        //process plugins

        //load module
        $module = PKernel::$params['module'];
        $moduledir = PKernel::$configuration->get('root/paths')->name('modules@path');

        //try to load module
        $path = $moduledir.'/'.$module.'/actiongroups/'.PKernel::$params['action'].".actions.class.php";
        
        //detect if we run as server or from command line (pcc -f)
        //second case means that we haven't to load any librairie
        if(isset($_SERVER['SERVER_SOFTWARE']) && dl("lib".$module)===FALSE){
            throw new PException("Unable to load module named ".$module);
        }

        //then include path to module
        include $path;

        //process module action
        $actiongroup = PKernel::$params['group']."Actions";
        $actiongroup = new $actiongroup();
        $actionReturn = $actiongroup->process();
        //process the action return exeuction
        PProcess::execute($actionReturn);

    }


    /**
     * Get single instance of kernel
     *
     * @return PKernel instance
     */
    public function instance($configfile){
        if(PKernel::$_instance===NULL || $configfile!==NULL){
            PKernel::$_instance = new PKernel($configfile);
        }
        return PKernel::$_instance;
    }

    /**
     * Get configuration accessor
     * 
     * @return PXML_struct
     */
    public function getConfiguration(){
        return PKernel::$configuration;
    }


    /**
     * Get kernel parameter as module name, action name...
     *
     * @param string $parameterName
     * @return string
     */
    public function getParam($name){
        return PKernel::$params[$name];
    }

    /**
     *  Check if called module is installed
     *
     * @return boolean
     */
    private function checkModuleInstallation(){
        return true;
    }

}
